#include "WriterCatalog.h"



namespace fs = boost::filesystem;

	namespace glog{
		CLogWriter::CLogWriter(const std::string fileName, LogLevel lvl):m_fileName(fileName), m_level(lvl)
		{
			fs::path full_path = fs::system_complete(fs::path(fileName.c_str()));
			fs::path parentPath = full_path.parent_path();
		
			if (!fs::is_directory(parentPath))
			{
				pathCreator(parentPath);
			}
			
			m_file.open(fileName.c_str(), std::ios_base::out /*|std::ios_base::app*/);
			assert(!m_file.fail());
		}
		//TODO: Would this method be more general and live outside this class??
		bool CLogWriter::pathCreator( boost::filesystem::path path_to_create )
		{
			if (!fs::exists(path_to_create))
			{
				pathCreator(path_to_create.parent_path());
			}
			//if not create the directory
			fs::create_directory(path_to_create);
			return true;
		}
		CLogWriter::~CLogWriter(void)
		{
			m_file << "closing file" << std::endl;
			m_file.close();
		}



		void CLogWriter::writeToLog(const std::string& loggerGroup, LogLevel level, time_t time,const std::string& message)
		{
			if (level >= m_level)
			{
				m_file<< "["<< levelToString(level)<<"]"<< message << "\n";
				m_file.flush();
			}
		}
	};
